package cn.rengy.tpa.wxmp.component;


import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.io.IoUtil;
import cn.rengy.tool.core.util.JaxbUtils;
import cn.rengy.tpa.wxmp.aes.WXBizMsgCrypt;
import cn.rengy.tpa.wxmp.event.AuthType;
import cn.rengy.tpa.wxmp.exception.WeChatException;
import cn.rengy.tpa.wxmp.param.SignatureParam;


public abstract class WechatComponentSupport {
	Logger logger = LoggerFactory.getLogger(WechatComponentSupport.class);
	
	public void dispatch(SignatureParam signatureParam,InputStream inputStream) throws Exception{
		//HttpServletRequest request;
		/*for (Enumeration<String> eh = request.getParameterNames(); eh.hasMoreElements();){
			String parName = eh.nextElement();
			String parValue = request.getParameter(parName);
			logger.debug("{}={}",parName,parValue);
		}*/
		
		String signature =signatureParam.getSignature();
		String timestamp = signatureParam.getTimestamp();
		String nonce = signatureParam.getNonce();
		String echostr = signatureParam.getEchostr();
		
		//String encrypt_type=request.getParameter("encrypt_type");
		String msg_signature=null;//request.getParameter("msg_signature");
		String xmlBody=null;
		String appid=null;
		try {
			//String postData = StreamUtils.streamToString(input);
			String postData=IoUtil.read(inputStream,StandardCharsets.UTF_8);
			logger.debug("授权事件报文:{}",postData);
			AuthMsg authMsg=(AuthMsg)JaxbUtils.xmlToBean(AuthMsg.class, postData);
			String encrypt = authMsg.getEncrypt();
			appid=authMsg.getAppid();
			ComponentApp app=getComponentApp(appid);
			WXBizMsgCrypt pc = new WXBizMsgCrypt(app.getToken(), app.getAeskey(), appid);
			xmlBody = pc.decryptMsg(msg_signature, timestamp, nonce, encrypt);
			logger.debug("解密后报文:{}",xmlBody);
		} catch (Exception e) {
			logger.error("post data deal failed!");
			throw e;
		}
		//根据不同的InfoType处理不同的事件
		AuthBody authBody=(AuthBody)JaxbUtils.xmlToBean(AuthBody.class, xmlBody);
		AuthType authType = AuthType.valueOf(authBody.getInfoType());
		switch (authType) {
			case authorized:
				authorized(authBody);
				break;
			case unauthorized:
				unauthorized(authBody);
				break;
			case updateauthorized:
				updateauthorized(authBody);
				break;
			case component_verify_ticket:
				component_verify_ticket(authBody);
				break;
			default:
				throw new WeChatException("未知的授权通知类型");
		}
	}
	/**
	 * 推送component_verify_ticket协议
	 * @param appid
	 * @return
	 */
	protected abstract void component_verify_ticket(AuthBody authBody);
	/**
	 * 授权成功通知
	 * @return
	 */
	protected abstract void authorized(AuthBody authBody);
	/**
	 * 取消授权通知
	 */
	protected abstract void unauthorized(AuthBody authBody);
	
	/**
	 * 授权更新通知
	 */
	protected abstract void updateauthorized(AuthBody authBody);
	/**
	 * 得到第三方平台配置信息
	 * @param appid
	 * @return
	 */
	protected abstract ComponentApp getComponentApp(String appid);
}
